<!--
 * @Description: 
 * @Author: mmzhang   tabPane 用v-if会导致顺序错乱  需要加index  注意index大于0
 * @Date: 2022-12-19 15:05:51
 * @LastEditTime: 2023-02-15 14:13:21
-->
<template>
  <div class="task-page">
    <Tabs type="card" v-model="showTab" :animated="false">
      <TabPane
        :index="1"
        :label="`我的待办（${taskInfo.myTaskNumbers || 0}）`"
        name="myUDealTask"
        v-if="isbuildFlag == 'buildpage' ? unlocking('0108080000') : unlocking('0305000000')"></TabPane>
      <TabPane
        :index="2"
        :label="`${isbuildFlag == 'buildpage' ? '案场待办' : '其他人员待办'}（${
          isbuildFlag == 'buildpage' ? taskInfo.buildingTaskNumbers : taskInfo.adminNumbers || 0
        }）`"
        name="buildUDealTask"
        v-if="isbuildFlag == 'buildpage' ? unlocking('0108090000') : unlocking('0306000000')"></TabPane>
      <TabPane
        :index="3"
        :label="`我的流程审批（${taskInfo.processApprovalNumbers || 0}）`"
        name="processApproval"
        v-if="isbuildFlag == 'buildpage' ? unlocking('0108100000') : unlocking('0307000000')"></TabPane>
      <TabPane
        :index="4"
        :label="`他人流程审批（${taskInfo.otherProcessApprovalNumbers || 0}）`"
        name="otherProcessApproval"
        v-if="isbuildFlag == 'buildpage' ? unlocking('0108120000') : unlocking('0309000000')"></TabPane>
    </Tabs>
    <Button
      @click="myPointFun"
      type="primary"
      class="score-btn"
      v-if="isbuildFlag == 'nobuildpage' && unlocking('0310000000')">
      我的积分
    </Button>
    <!--待办-->
    <Udeal-task-page
      ref="UdealTaskPage"
      v-if="showTab == 'myUDealTask' || showTab == 'buildUDealTask'"
      @onTaskInfo="getTaskHeadInfoForWeb"></Udeal-task-page>
    <!--流程审批-->
    <process-page
      ref="processPage"
      v-if="showTab == 'processApproval' || showTab == 'otherProcessApproval'"
      @onTaskInfo="getTaskHeadInfoForWeb"
      :isOtherProcess="showTab == 'processApproval' ? false : true"></process-page>
    <dt-loading :loading="loading"></dt-loading>
    <!--我的积分-->
    <my-point ref="myPoint"></my-point>
  </div>
</template>
<script>
  import UdealTaskPage from '../../../UdealTask/index.vue'
  import processPage from './processPage.vue'
  import homeMgr from '@/biz/buildingHome/home'
  import workMgr from '@/biz/work/index'
  import myPoint from './myPoint.vue'

  export default {
    data() {
      return {
        showTab: 'myUDealTask',
        taskInfo: {
          buildingInfo: [],
          buildingNumbers: 0,
          adminNumbers: 0,
          buildingTaskNumbers: 0,
          myTaskNumbers: 0,
          processApprovalNumbers: 0,
          taskNumbers: 0,
          otherProcessApprovalNumbers: 0,
          transmitTaskCount: 0
        },
        isbuildFlag: 'buildpage',
        loading: false
      }
    },
    watch: {
      showTab(val, oldVal) {
        if (val == 'processApproval' || val == 'otherProcessApproval') {
          this.$nextTick(function () {
            this.$refs.processPage.searchFun()
          })
        } else {
          this.$nextTick(function () {
            this.getTaskHeadInfoForWeb()
            this.$refs.UdealTaskPage.resetTaskFun()
            this.$refs.UdealTaskPage.buildInitFun(val)
          })
        }
      },
      $route(to, from) {
        // 路由改变要刷新数据，页面不刷
        if (to.name == 'taskListPage' || to.name == 'taskListPageWork') {
          this.showTab = this.$route.params.showTab || this.showTab
          this.getTaskHeadInfoForWeb()

          if (this.showTab == 'processApproval' || this.showTab == 'otherProcessApproval') {
            this.$nextTick(function () {
              this.$refs.processPage.searchFun()
            })
          } else {
            this.$nextTick(function () {
              this.$refs.UdealTaskPage.buildInitFun(this.showTab)
            })
          }
        }
      }
    },
    mounted() {
      this.getTaskHeadInfoForWeb()
      // 判断权限
      let showTabAuth = 'myUDealTask'
      if (this.isbuildFlag == 'buildpage') {
        // 案场内
        if (this.unlocking('0108120000')) {
          showTabAuth = 'otherProcessApproval'
        }
        if (this.unlocking('0108100000')) {
          showTabAuth = 'processApproval'
        }
        if (this.unlocking('0108090000')) {
          showTabAuth = 'buildUDealTask'
        }
        if (this.unlocking('0108080000')) {
          showTabAuth = 'myUDealTask'
        }
      } else {
        if (this.unlocking('0309000000')) {
          showTabAuth = 'otherProcessApproval'
        }
        if (this.unlocking('0307000000')) {
          showTabAuth = 'processApproval'
        }
        if (this.unlocking('0306000000')) {
          showTabAuth = 'buildUDealTask'
        }
        if (this.unlocking('0305000000')) {
          showTabAuth = 'myUDealTask'
        }
      }
      this.showTab = this.$route.params.showTab || showTabAuth
      if (this.showTab == 'processApproval' || this.showTab == 'otherProcessApproval') {
        this.$nextTick(function () {
          this.$refs.processPage.searchFun()
        })
      } else {
        this.$nextTick(function () {
          this.$refs.UdealTaskPage.resetTaskFun()
          this.$refs.UdealTaskPage.buildInitFun(this.showTab)
        })
      }
    },
    components: {
      UdealTaskPage,
      processPage,
      myPoint
    },
    methods: {
      // 待办任务
      getTaskHeadInfoForWeb() {
        this.loading = true
        this.isbuildFlag = this.$tool.getStorage('isbuildFlag')
        let params = {}
        let req = ''
        if (this.isbuildFlag == 'buildpage') {
          params = {
            buildingId: this.$dt.buildInfo().housesId
          }
          req = homeMgr.getTaskHeadInfoForWeb(params)
        } else {
          req = workMgr.getTaskHeadInfoForOrg(params)
        }

        req
          .then((res) => {
            this.taskInfo = res
            if (this.showTab != 'processApproval' && this.showTab != 'otherProcessApproval') {
              this.$refs.UdealTaskPage.buildTaskInfo(this.taskInfo)
            }
            this.loading = false
          })
          .catch((err) => {
            this.taskInfo = {
              buildingInfo: [],
              buildingNumbers: 0,
              adminNumbers: 0,
              buildingTaskNumbers: 0,
              myTaskNumbers: 0,
              processApprovalNumbers: 0,
              taskNumbers: 0,
              otherProcessApprovalNumbers: 0,
              transmitTaskCount: 0
            }
            if (this.showTab != 'processApproval' && this.showTab != 'otherProcessApproval') {
              this.$refs.UdealTaskPage.buildTaskInfo(this.taskInfo)
            }
            this.loading = false
          })
      },
      myPointFun() {
        this.$refs.myPoint.show()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .task-page {
    padding: 20px;
    .score-btn {
      position: absolute;
      right: 20px;
      top: 80px;
    }
  }
</style>
